package com.example.controller;

import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;


import java.io.IOException;

import static com.example.utils.LocalFileUpload.getBase64Image;

@WebServlet("/upload")
@MultipartConfig
public class OssFileUploadServlet extends HttpServlet {

    /**
     * 上传图片
     *
     * @param req
     * @param resp
     * @throws Exception
     */
    public void service(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {

        req.setCharacterEncoding("utf-8");

        Part file = req.getPart("file");
//        String fileName = file.getSubmittedFileName();
//        String imageUrl = OssFileUpload(fileName, file.getInputStream());
        String base64Image = getBase64Image(file);
        //返回结果
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(JSON.toJSONString(base64Image));
    }
}
